/**
 * Created by red on 2017/4/30.
 */
import { parse } from 'utils/';
/**
 * 文章管理
 * @type {Object}
 */
export default [
  {
    name: '书籍列表',
    method: 'bookList',
    type: 'parse',
    excute: ({ data, fn, errFn } = {}) => {
      parse.query(
        'books',
        query => {
          query.count().then(res => data.page.total = res, err => data.page.total = 0);
          query.skip((data.page.current_page - 1) * data.page.page_size);
          query.limit(data.page.page_size);
        }
      ).filter(x => x.type && x.type === 'result')
        .map(x => {
          return x.result.map(item => ({
            id: item.id,
            name: item.get('name'),
            describe: item.get('describe'),
            cover: item.get('cover')._url,
            price: item.get('price'),
            chapter_count: item.get('chapter_count'),
            author: item.get('author'),
            word_count: item.get('word_count'),
            grade_type: item.get('grade_type'),
          }));
        })
        .subscribe(fn, errFn);
    }
  },
  {
    name: '查询文章',
    method: 'findBook',
    type: 'parse',
    excute: ({ data, fn, errFn } = {}) => {
      parse.query('books', query => query.equalTo('objectId', data))
        .filter(x => x.type && x.type === 'result')
        .map(x => {
          return x.result.map(item => ({
            id: item.id,
            name: item.get('name'),
            describe: item.get('describe'),
            cover: item.get('cover'),
            price: item.get('price'),
            chapter_count: item.get('chapter_count'),
            author: item.get('author'),
            word_count: item.get('word_count'),
            grade_type: item.get('grade_type'),
          }));
        })
        .subscribe(next => fn(next[0]));
    }
  },
  {
    name: '新增文章',
    method: 'createBook',
    type: 'parse',
    excute: ({ data, fn, errFn } = {}) => {
      parse.create('books', data).subscribe(fn, errFn);
    }
  },
  {
    name: '修改文章',
    method: 'updateBook',
    type: 'parse',
    excute: ({ data, fn, errFn } = {}) => {
      parse.query('books', query => query.equalTo('objectId', data.id))
        .filter(x => x.type && x.type === 'result')
        .map(x => x.result[0])
        .subscribe(res => {
          parse.update(res, data.data).subscribe(fn, errFn);
        });
    }
  },
  {
    name: '删除文章',
    method: 'deleteArticle',
    type: 'parse',
    excute: ({ data, fn, errFn } = {}) => {
      parse.delete(data).subscribe(fn, errFn);
    }
  }
];